package com.vrv;

import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson.JSONObject;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.FlowExecutorHolder;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.property.LiteflowConfig;

import java.util.HashMap;
import java.util.concurrent.*;

/**
 * @author Bang
 * @ClassName: Main
 * @Desc: TODO
 * @Date: 2023/11/02 17:42
 * @Version: 1.0
 * @Modified By:
 */
public class Main {
    public static void main(String[] args) throws Exception {
        int TIMES = 100000;
        test1(TIMES);
        test2(TIMES);

        LiteflowConfig config = getConfig();
        FlowExecutor flowExecutor = FlowExecutorHolder.loadInstance(config);
        String chain = "chain11";
        String param = "";
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chain, param, JSONObject.class);
        JSONObject contextBean = liteflowResponse.getContextBean(JSONObject.class);

    }

    private static void test1(int TIMES) throws InterruptedException {
        LiteflowConfig config = getConfig();
        FlowExecutor flowExecutor = FlowExecutorHolder.loadInstance(config);
        String chain = "test-kafka";
        String param = "";

        StopWatch stopWatch = StopWatch.create("");
        stopWatch.start();

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CountDownLatch countDownLatch = new CountDownLatch(TIMES);
        for (int i = 0; i < TIMES; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chain, param, JSONObject.class);
                        JSONObject contextBean = liteflowResponse.getContextBean(JSONObject.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });

        }
        countDownLatch.await();
        stopWatch.stop();
        System.out.println(stopWatch.getTotal(TimeUnit.SECONDS));
        System.out.println(stopWatch.getTotal(TimeUnit.MILLISECONDS));
        System.out.println(stopWatch.getTotal(TimeUnit.MICROSECONDS) / TIMES);

        executorService.shutdown();
    }

    private static void test2(int TIMES) throws Exception {
        LiteflowConfig config = getConfig();
        FlowExecutor flowExecutor = FlowExecutorHolder.loadInstance(config);
        String chain = "test-kafka";
        String param = "";

        StopWatch stopWatch = StopWatch.create("");
        stopWatch.start();
        for (int i = 0; i < TIMES; i++) {
            LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chain, param, JSONObject.class);
            JSONObject contextBean = liteflowResponse.getContextBean(JSONObject.class);

        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotal(TimeUnit.SECONDS));
        System.out.println(stopWatch.getTotal(TimeUnit.MILLISECONDS));
        System.out.println(stopWatch.getTotal(TimeUnit.MICROSECONDS) / TIMES);
    }

    private static LiteflowConfig getConfig() {
        LiteflowConfig config = new LiteflowConfig();
//规则文件路径
        config.setRuleSource("config/flow.el.xml");
        HashMap<String, String> ruleSourceExtDataMap = new HashMap<>();
        config.setRuleSourceExtDataMap(ruleSourceExtDataMap);
//-----------------以下非必须-----------------
//liteflow是否开启，默认为true
        config.setEnable(true);
//liteflow的banner打印是否开启，默认为true
        config.setPrintBanner(true);
//zkNode的节点，只有使用zk作为配置源的时候才起作用，默认为/lite-flow/flow
//上下文的最大数量槽，默认值为1024
        config.setSlotSize(1024);
//FlowExecutor的execute2Future的线程数，默认为64
        config.setMainExecutorWorks(64);
//FlowExecutor的execute2Future的自定义线程池Builder，LiteFlow提供了默认的Builder
        config.setMainExecutorClass("com.yomahub.liteflow.thread.LiteFlowDefaultMainExecutorBuilder");
//自定义请求ID的生成类，LiteFlow提供了默认的生成类
        config.setRequestIdGeneratorClass("com.yomahub.liteflow.flow.id.DefaultRequestIdGenerator");
//并行节点的线程池Builder，LiteFlow提供了默认的Builder
        config.setThreadExecutorClass("com.yomahub.liteflow.thread.LiteFlowDefaultWhenExecutorBuilder");
//异步线程最长的等待时间(只用于when)，默认值为15000
        config.setWhenMaxWaitTime(15000);
//异步线程最长的等待时间(只用于when)，默认值为MILLISECONDS，毫秒
        config.setWhenMaxWaitTimeUnit(TimeUnit.MILLISECONDS);
//when节点全局异步线程池最大线程数，默认为16
        config.setWhenMaxWorkers(16);
//when节点全局异步线程池等待队列数，默认为512
        config.setWhenQueueLimit(512);
//并行循环子项线程池最大线程数，默认为16
        config.setParallelMaxWorkers(16);
//并行循环子项线程池等待队列数，默认为512
        config.setParallelQueueLimit(512);
//并行循环子项的线程池Builder，LiteFlow提供了默认的Builder
        config.setParallelLoopExecutorClass("com.yomahub.liteflow.thread.LiteFlowDefaultParallelLoopExecutorBuilder");
//是否在启动的时候就解析规则，默认为true
        config.setParseOnStart(true);
//全局重试次数，默认为0
        config.setRetryCount(0);
//是否支持不同类型的加载方式混用，默认为false
        config.setSupportMultipleType(false);
//全局默认节点执行器
        config.setNodeExecutorClass("com.yomahub.liteflow.flow.executor.DefaultNodeExecutor");
//是否打印执行中过程中的日志，默认为true
        config.setPrintExecutionLog(true);
//是否开启本地文件监听，默认为false
        config.setEnableMonitorFile(false);
//简易监控配置选项
//监控是否开启，默认不开启
        config.setEnableLog(false);
//监控队列存储大小，默认值为200
        config.setQueueLimit(200);
//监控一开始延迟多少执行，默认值为300000毫秒，也就是5分钟
        config.setDelay(300000L);
//监控日志打印每过多少时间执行一次，默认值为300000毫秒，也就是5分钟
        config.setPeriod(300000L);
        return config;
    }
}
